import os

#功能：读取文本文件的每一行的英语语句数据，每行最多lineMaxChar个字符，多余的字符会被舍弃掉，但会保留每一个英语单词的完整性
#例子：
# Life is fucking movie   lineMaxChar= 20
# 输出：Life is fucking

# filePath:读取文本文件的路径，lineMaxChar:最大的字符个数，writefilePath生成文本文件路径
def main(filePath,lineMaxChar,writefilePath):
    # 存储每行数据的容器
    ingword = []
    file = open(filePath, encoding='utf-8')
    lines = file.read().splitlines()

    for line in lines:
        # print(line)
        wordList = line.split(" ");
        tempStr = "";
        for word in wordList:
            # print(word)
            temp = tempStr
            temp += word + " "
            if len(temp) > lineMaxChar:
                break
            else:
                tempStr = temp
        ingword.append(tempStr)

    # 写之前，先检验文件是否存在，存在就删掉
    if os.path.exists(writefilePath):
        os.remove(writefilePath)

    # 以写的方式打开文件，如果文件不存在，就会自动创建
    file_write_obj = open(writefilePath, 'w', encoding="utf-8")  # 新文件
    for var in ingword:
        print(var)
        file_write_obj.write(var)  # 逐行写入
        file_write_obj.write('\n')
    file_write_obj.close()
    print("保存文件成功")



if __name__ == '__main__':
    # 配置
    # 读取的文本文件
    filePath = "数据测试集\\英语文本.txt"
    # 每行最大的字符数字
    lineMaxChar = 55
    # 生成的文件
    writefillePath = "数据结果集\\英语文本result.txt"


    main(filePath, lineMaxChar, writefillePath)